<?php

use App\Kuafu\Base\Model\BaseModel;
use App\Kuafu\WaiMai\Model\WaiMaiOrderDetailModel;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Jialeo\LaravelSchemaExtend\Schema;

class CreateWaimaiOrderDetailsSplitTable extends Migration
{
    private $name;

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $cFunc = function (Blueprint $table) {
            $table->unsignedBigInteger('id')->primary();
            $table->unsignedInteger('brandId')->default(0);
            $table->unsignedInteger('shopId')->default(0);
            $table->unsignedBigInteger('orderId')->default(0);
            $table->unsignedInteger('productType')->default(1)->comment('1菜 2包装物');
            $table->string('productName', 32)->default('')->comment('菜品名称');
            $table->string('productProperty', 64)->default('')->comment('菜品属性');
            $table->string('unit', 8)->default('')->comment("菜品单位");
            $table->unsignedInteger('originalUnitPrice')->default(0)->comment("原单价");
            $table->unsignedInteger('originalPrice')->default(0)->comment("原总价");
            $table->unsignedInteger('num')->default(0)->comment("菜品数量");
            $table->unsignedInteger('payPrice')->default(0)->comment("应付总价");
            $table->unsignedInteger('discountPrice')->default(0)->comment("该行的优惠金额");
            $table->unsignedInteger('incomes')->default(0)->comment("单行菜的实收");
            $table->unsignedInteger('createdAt')->default(0)->comment('创建时间');
            $table->unsignedInteger('updatedAt')->default(0)->comment('更新时间');

            $table->index('orderId', 'orderId_idx');
            $table->index('createdAt', 'createdAt_idx');
            $table->index('payPrice', 'payPrice_idx');
            $table->index('shopId', 'idx_shopId');

            $table->collation = 'utf8mb4_general_ci';
            $table->comment   = '外卖菜品详情表' . $this->name;
        };

        // 分表库
        for ($i = 0; $i <= 99; $i++) {
            $this->name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlWaiMai")->create('waimai_order_details' . $this->name, $cFunc);
        }

        // 融合库
         // Schema::connection('mysqlWaiMaiAll')->create('waimai_order_details', $cFunc);


    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        for ($i = 0; $i <= 99; $i++) {
            $this->name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlWaiMai")->dropIfExists('waimai_order_details' . $this->name);
        }

//        Schema::connection('mysqlWaiMaiAll')->drop('waimai_order_details');

        Schema::connection('mysql')->table('waimai_order_details', function (Blueprint $table) {
            $table->integer('incomesDiscount')->default(0);
        });
    }
}
